import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiService, JsonResult } from './api.service';
import { PageVo, CVEListVo, CVEDetailVo, CVECount, CVEListsVo, CNNVDListsVo, AddVo, ScoreV2, ScoreV3 } from 'src/app/shared/models';

@Injectable({
  providedIn: 'root'
})
export class CveService {
  resData = {};
  data = {};

  constructor(private apiService: ApiService) { }

  getCVEPage(pageIndex: number = 1, pageSize: number = 10, filter: string): Observable<PageVo<CVEListVo>> {
    console.log('执行方法');
    if (filter !== null && filter !== undefined && filter !== '') {
      if (filter.length > 1 && filter.length < 50) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', filter);
        return this.apiService.get('/cves', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<CVEListVo>>) => jsonResult.data)
          );
      }
    } else {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      return this.apiService.get('/cves', { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<CVEListVo>>) => jsonResult.data)
        );
    }
  }

  getCVEFilterPage(filter: string, pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<CVEListVo>> {
    console.log('执行方法');
    const params = new HttpParams()
      .append('filter', filter)
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/cves', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<CVEListVo>>) => jsonResult.data)
      );
  }

  /**
   * 获取cve的详细信息
   * @param cveName 传输的数据
   */
  getCVEDetailPage(cveName: string): Observable<PageVo<CVEDetailVo>> {
    console.log('执行方法');
    return this.apiService.get('/cvedetail/' + cveName, {})
      .pipe(
        map((jsonResult: JsonResult<PageVo<CVEDetailVo>>) => jsonResult.data)
      );
  }

  getCVEPageByAppId(appUuid: string, pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<CVEListVo>> {
    const params = new HttpParams()
      .append('appUuid', `${appUuid}`)
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/applications/cvelist', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<CVEListVo>>) => jsonResult.data)
      );
  }

  /** 获取cve的总数和使用数*/
  getCVEConut(): Observable<CVECount> {
    console.log('得到使用数和总数');
    return this.apiService.get('/data/cveandcnnvd/count')
      .pipe(
        map((jsonResult: JsonResult<CVECount>) => jsonResult.data)
      );
  }

  getCveList(pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<CVEListsVo>> {
    const params = new HttpParams()
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/data/cve/list', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<CVEListsVo>>) => jsonResult.data)
      );
  }

  getCnnvdList(pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<CNNVDListsVo>> {
    const params = new HttpParams()
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/data/cnnvd/list', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<CNNVDListsVo>>) => jsonResult.data)
      );
  }

  addCVEAndCNNVD(info: any, type: string) {
    console.log('== info ==', info);
    if (type === 'CVE') {
      this.resData = {
        'cveInfo': info,
        'type': type
      };
    }

    if (type === 'CNNVD') {
      this.resData = {
        'cnnvdInfo': info,
        'type': type
      };
    }

    console.log('==  this.resData ==', this.resData);
    return this.apiService.post('/data/cveandcnnvd/add', this.resData).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult)
    );
  }

  addRelevance(info: any) {
    const infoData = [];
    console.log('== 添加关联组件  this.info ==', info, infoData);
    infoData.push(info);
    this.data = {
      'cpeInfoList': infoData
    };

    this.resData = {
      'cpeInfo': JSON.stringify(this.data)
    };

    console.log('=== this.infoData ===>', infoData);
    console.log('=== this.resData ===>', this.resData);
    return this.apiService.post('/data/cveandcnnvd/add', this.resData).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult)
    );
  }

  uploadFile(file: FormData) {
    console.log('=== uploadFile.file ===>', file);
    return this.apiService.post('/data/cveandcnnvd/upload', file).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult)
    );
  }





  // 添加CVE
  addCveAndCnnvdInfo(data: any) {
    console.log('==  this.resData ==', data);
    return this.apiService.post('/data/cveandcnnvd/add', data).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult)
    );
  }
  // 获取v2score
  getV2Score(resData: any){
    console.log('==  this.resData ==', resData);
    return this.apiService.post('/data/cvssv2/score', resData).pipe(
      map((jsonResult: JsonResult<ScoreV2>) => jsonResult)
    );
  }
  // 获取v3score
  getV3Score(resData: any) {
    console.log('==  this.resData ==', resData);
    return this.apiService.post('/data/cvssv3/score', resData).pipe(
      map((jsonResult: JsonResult<ScoreV3>) => jsonResult)
    );
  }

}
